{include file='header'/}
<body class="pear-container">
<div class="layui-card">
    <div class="layui-card-body">
        <div class="layui-row hui-form-search">
            <form class="layui-form layui-col-md12" action="javascript:;" id="form_admin_search">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <input type="text" name="name" id="name" placeholder="请输入字段名" autocomplete="off"
                               class="layui-input">
                    </div>
                    <div class="layui-inline" style="width:120px;">
                        <select name="status" id="status">
                            <option value="">==是否启用==</option>
                            <option value=1>已启用</option>
                            <option value="0">已禁用</option>
                        </select>
                    </div>
                    <div class="layui-inline">
                        <button class="layui-btn layui-btn-md" data-type="reload" lay-submit="" lay-filter="search">
                            <i class="layui-icon layui-icon-search"></i>搜索
                        </button>
                        <button class="layui-btn layui-btn-danger layui-btn-md reset" type="reset">
                            <i class="layui-icon">&#xe63f;</i>清空
                        </button>
                    </div>

                </div>
            </form>
        </div>
    </div>
</div>
<div class="layui-card">
    <div class="layui-card-body">
        <table id="model_table" lay-filter="model_table"></table>
    </div>
</div>
<script type="text/html" id="add_toolbar">
    <button class="pear-btn pear-btn-primary pear-btn-md"
            data-open="{:url('models_field/add')}"
            data-title="添加模型字段" data-reload="2">
        <i class="layui-icon layui-icon-add-1"></i>
        新增
    </button>
</script>

<script type="text/html" id="statusTpl">
    <input type="checkbox" name="status" value="{{d.id}}" lay-skin="switch" lay-text="启用|禁用"
           lay-filter="switchStatus"
           data-href="{:url('models_field/modify')}?id={{d.id}}&field=status" {{d.status== 1 ? 'checked' : '' }} />
</script>

<script type="text/html" id="barDemo">
    <button class="pear-btn pear-btn-primary pear-btn-xs" title="编辑"
            data-open="{:url('models_field/edit')}?id={{d.id}}"
            data-title="编辑模型字段" data-reload="2">
        <i class="layui-icon layui-icon-edit"></i>
    </button>
    <button class="pear-btn pear-btn-danger pear-btn-xs" title="删除"
            data-delete="{:url('models_field/delete')}?id={{d.id}}"
            data-title="您确定要删除吗？" data-reload="2">
        <i class="layui-icon layui-icon-delete"></i>
    </button>
</script>

{include file='footer'/}
<script>
  layui.use(['table', 'jquery', 'drawer'], function () {
    var $ = layui.jquery;
    var table = layui.table;
    table.render({
      elem: '#model_table'
      , toolbar: '#add_toolbar'
      , defaultToolbar: [{
        layEvent: 'refresh',
        icon: 'layui-icon-refresh',
        title: '刷新'
      }, 'filter', 'exports']
      , method: 'get'
      , where: {model_id:'{$model_id}'}
      , url: "{:url('models_field/index')}"
      , parseData: function (res) {
        return {
          "code": 0,
          "msg": res.msg,
          "count": res.count,
          "data": res.data
        };
      }
      , cellMinWidth: 100
      , cols: [[
        {field: 'sort', title: '排序', width: 60, align: 'center'}
        , {field: 'model_name', title: '所属模型', width: 150}
        , {field: 'name', title: '字段名称'}
        , {field: 'title', title: '字段标题'}
        , {field: 'type', title: '字段类型',width: 150}
        , {
          field: 'is_required', title: '是否必填', width: 80, align: 'center', templet: function (d) {
            if (d.is_required === 1) {
              return '<span class="layui-badge layui-bg-green">是</span>';
            } else {
              return '<span class="layui-badge layui-bg-orange">否</span>';
            }
          }
        }
        , {field: 'status', title: '状态', templet: '#statusTpl', align: 'center', width: 100}
        , {fixed: 'right', title: '操作', toolbar: '#barDemo', align: 'center', width: 130}
      ]]
      , id: 'testReload'
      , page: true
      , limit: 10
    });
      var $ = layui.$, active = {
          reload: function () {
              //执行重载
              table.reload('testReload', {
                  page: {curr: 1},
                  where: {
                      key: {
                          name: $('#name').val(),
                          status: $('#status').val(),
                      }
                  }
              });
          }
      };

      $('.hui-form-search .layui-btn').on('click', function () {
          var type = $(this).data('type');
          active[type] ? active[type].call(this) : '';
      });
      $('.hui-form-search .reset').on('click', function () {
          window.location.reload();
      });

      //监听行工具事件
      table.on('toolbar(model_table)', function (obj) {
          if (obj.event === 'refresh') {
              table.reload('testReload');
          }
      });

  });
</script>
</body>
</html>
